#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int N = 150010;

int t;
int n, l;
int a[N];
// int mdis[N];

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    cin >> t;
    while (t--) {
        cin >> n;
        unordered_map<int, vector<int> > mp;
        map<int, int> mdis;
        for (int i = 1;i <= n;i++) {
            cin >> a[i];
            if (mp[a[i]].size() > 0) {
                int lst = mp[a[i]].back();
                if (mdis[i])
                    mdis[i] = min(mdis[i], i-lst);
                else
                    mdis[i] = i-lst;
            }
            mp[a[i]].push_back(i);
        }

        int ans = 0;
        if (mp.size() == n) {
            puts("-1");
            continue;
        }

        for (auto [k, v] : mdis) {
            if (v != 1e9) {
                ans = max(ans, n-v);
            }
        }
        cout << ans << endl;
    }
    return 0;
}